package priv.pront.code.national.acwing.dp.bags;

import java.util.Scanner;

public class Acwing423_采药 {

    static int T, M;
    static int[] t, w;
    static int[] dp;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        T = scanner.nextInt(); M = scanner.nextInt();
        t = new int[M + 1];
        w = new int[M + 1];
        for(int i = 1; i <= M; i++){
            t[i] = scanner.nextInt();
            w[i] = scanner.nextInt();
        }
        dp = new int[T + 1];
        for(int i = 1; i <= M; i++){
            for(int j = T; j >= 1; j--){
                dp[j] = dp[j];
                if(j - t[i] >= 0){
                    dp[j] = Math.max(dp[j], dp[j - t[i]] + w[i]);
                }
            }
        }
        System.out.println(dp[T]);
    }
}
